﻿using System;
using System.Linq;
using System.Windows.Input;
using GalaSoft.MvvmLight.Command;
using Microsoft.Practices.Prism.Regions;
using Microsoft.Practices.Prism.ViewModel;
using Microsoft.Practices.Unity;
using Mipic.Infrastructure;
using Mipic.Modules.Mood.Views;

namespace Mipic.Modules.Mood.ViewModels
{
    public class MoodViewModel : NotificationObject, IConfirmNavigationRequest
    {
        
        #region //--------- Private Fields --------------//
        
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        
        #endregion
        
        #region //--------- Public Constructors ---------//
        
        public MoodViewModel(IRegionManager regionManager, IUnityContainer container)
        {
            _container = container;
            _regionManager = regionManager;
        }
        
        #endregion 
        
        #region //--------- Public Commands -------------//
        
        public ICommand GoBackCommand
        {
            get
            {
                return new RelayCommand(
                    () => _regionManager.RequestNavigate(
                        RegionNames.MainRegionName, "ConfigurationView"));
            }
        }
        
        public ICommand NavigateBackCommand
        {
            get
            {
                return new RelayCommand(
                    () => _regionManager.RequestNavigate(RegionNames.MainRegionName, "ConfigurationView"));
            }
        }
        
        #endregion
        
        #region //--------- Interface Members -----------//
        
        /// <summary>
        /// Confirms the navigation request.
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        /// <param name="continuationCallback">The continuation callback.</param>
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
        {
            continuationCallback(true);
        }
        
        /// <summary>
        /// Called to determine if this instance can handle the navigation request.
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        /// <returns>
        /// <see langword="true" /> if this instance accepts the navigation request; otherwise,
        /// <see langword="false" />.
        /// </returns>
        public bool IsNavigationTarget(NavigationContext navigationContext)
        {
            return true;
        }
        
        /// <summary>
        /// Called when the implementer is being navigated away from.
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedFrom(NavigationContext navigationContext)
        {
            // Not implement this method.
        }
        
        /// <summary>
        /// Called when the implementer has been navigated to.
        /// </summary>
        /// <param name="navigationContext">The navigation context.</param>
        public void OnNavigatedTo(NavigationContext navigationContext)
        {
            RegisterViewIfNeeded();

            string path = navigationContext.Parameters["SelectedImagePath"];
            
            var imageView = _regionManager.Regions[RegionNames.MoodViewImageRegionName].GetView("ImageView") as ImageView;
            imageView.ViewModel.SelectedImagePath = path;
        }
    
        #endregion
        
        #region //--------- Private Methods -------------//
        
        /// <summary>
        /// Registers the view if needed.
        /// </summary>
        private void RegisterViewIfNeeded()
        {
            // Image View
            var imageView = _regionManager.Regions[RegionNames.MoodViewImageRegionName].GetView("ImageView") as ImageView;
            if (imageView == null)
            {
                imageView = _container.Resolve<ImageView>();
                _regionManager.Regions[RegionNames.MoodViewImageRegionName].Add(imageView, "ImageView");
            }

            // Color Effect View
            var colorEffectView = _regionManager.Regions[RegionNames.MoodViewSideBarInfoRegionName].GetView("ColorEffectView") as ColorEffectView;
            if (colorEffectView == null)
            {
                colorEffectView = _container.Resolve<ColorEffectView>();
                _regionManager.Regions[RegionNames.MoodViewSideBarInfoRegionName].Add(colorEffectView, "ColorEffectView");
            }

            // Overlay View
            var overlayView = _regionManager.Regions[RegionNames.MoodViewSideBarInfoRegionName].GetView("OverlayView") as OverlayView;
            if (overlayView == null)
            {
                overlayView = _container.Resolve<OverlayView>();
                _regionManager.Regions[RegionNames.MoodViewSideBarInfoRegionName].Add(overlayView, "OverlayView");
            }

            // Control View
            var controlView = _regionManager.Regions[RegionNames.MoodViewSideBarButtonRegionName].GetView("ControlView") as ControlView;
            if (controlView == null)
            {
                controlView = _container.Resolve<ControlView>();
                _regionManager.Regions[RegionNames.MoodViewSideBarButtonRegionName].Add(controlView, "ControlView");
            }
        }

        #endregion

    }
}